<template>
  <div class="home">

    <div class="title">请在下面输入框中输入内容： <span class="addBtn" @click="addData">添加数据</span></div>
    <div class="input-warp center">
      <input class="input" type="text" v-model="inputContent"  placeholder="请输入一条数据">
    </div>

<!--    数据内容列表-->
    <div class="content-warp">
      <ul class="ul">
        <li class="li" ref="liList" v-for="(item,index) in todoList" :key="index">
          <div class="li-left">
            <input class="checkbox" type="checkbox" @click="pageSelectLength(index)" :checked="item.isChecked">
          </div>
          <div class="li-content">
            {{item.content}}
          </div>
          <div class="optBtn" @click="deleteList(index,item.id)">删除</div>
        </li>
      </ul>
    </div>

    <div class="bottom">
      <input  class="bottom-div" type="checkbox" v-model="isAllCheckedData" @click="handleIsCheckedAll">
      <div class="bottom-div">已经选择{{ pageSelectLength2 }}个</div>
      <div class="bottom-div">一共{{ pageAllDataLength }}个</div>
      <div class="bottom-div bottom-div2" @click="deleteSelectData()">删除所选择的数据</div>
    </div>
  </div>
</template>

<script>

export default {
  name: 'Home',
  components: {
  },
  data () {
    return {
      inputContent: '',
      selectCount: 0,
      allCount: 0,
      isAllChecked: false,
      todoList: [
        {
          id: 0,
          isChecked: false,
          content: '第一条数据'
        },
        {
          id: 1,
          isChecked: false,
          content: '第二条数据'
        },
        {
          id: 7,
          isChecked: false,
          content: '第七条数据'
        },
        {
          id: 2,
          isChecked: false,
          content: '第三条数据'
        },
        {
          id: 9,
          isChecked: false,
          content: '第九条数据'
        },
        {
          id: 4,
          isChecked: false,
          content: '第五条数据'
        },
        {
          id: 8,
          isChecked: false,
          content: '第二十一条数据'
        },
        {
          id: 5,
          isChecked: false,
          content: '第六条数据'
        }
      ]
    }
  },
  computed: {
    pageAllDataLength () {
      return this.todoList.length
    },
    pageSelectLength2 () {
      return this.todoList.reduce((pre, item) => {
        return pre + (item.isChecked ? 1 : 0)
      }, 0)
      //   forEach的方法
      // let count = 0
      // this.todoList.forEach((item2, index) => {
      //   if (item2.isChecked === true) {
      //     count++
      //   } else {
      //     // console.log(this.todoList)
      //   }
      // })
      // console.log(count, 'aaaaaaaa')
      // return count
    },
    /** 是否全选了 **/
    isAllCheckedData: {
      get () {
        return this.todoList.length === this.pageSelectLength2
      },
      // value 为isAllCheckedData的最新值
      set (value) {
        this.handleIsCheckedAll(value)
      }
    }
  },
  methods: {
    deleteList (index, id) {
      this.todoList.splice(index, 1)
      /** 瞎测试的根据去删除数据 **/
      // this.todoList.forEach((item, index) => {
      //   if (this.todoList[index].id === id) {
      //     console.log('删除的数据id>>>>>', id)
      //     console.log('删除的数据索引值>>>>>', index)
      //     this.todoList.splice(index, 1)
      //   } else {
      //   }
      // })
    },
    /** 添加数据  **/
    addData () {
      if (this.inputContent === '') {
        console.log('你还没有输入内容')
        return false
      } else {
        // 这个地方获取id的获取有问题，暂时这样了
        const newObj = {
          id: this.todoList.length + 1,
          content: this.inputContent,
          isChecked: false
        }
        this.todoList.push(newObj)
      }
    },
    /** 删除所选择的数据**/
    deleteSelectData () {
      this.todoList = this.todoList.filter(item => {
        return item.isChecked === false
      })
    },
    /** 点击切换全选或者全不选 **/
    handleIsCheckedAll (value) {
      this.todoList.forEach(item => {
        item.isChecked = value
      })
      // forEach的方法
      // if (value) {
      //   this.todoList.forEach((item3, index3) => {
      //     item3.isChecked = false
      //   })
      // } else {
      //   this.todoList.forEach((item3, index3) => {
      //     item3.isChecked = true
      //   })
      // }
    },
    /** 获取到已经选择的数量 **/
    // 这个方式并不好玩,会让后续的选择删除操作很
    pageSelectLength (index) {
      this.todoList[index].isChecked = !this.todoList[index].isChecked
      let count = 0
      this.todoList.forEach((item2, index) => {
        if (item2.isChecked === true) {
          count++
        } else {
          // console.log(this.todoList)
        }
      })
      this.selectCount = count
      return count
    }
  }
}
</script>
<style lang="scss" scoped>
  .center{
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .home{
    margin-top: 40px;
    .title{
      text-align: center;
      margin-bottom: 15px;
      .addBtn{
        border: #999999 1px solid;
        padding: 10px 20px;
      }
    }
    .input-warp{
      margin: auto;
      width: 600px;
      height: 60px;
      border: 1px solid;
      border-radius: 30px;
      .input{
        width: 550px;
        margin: auto;
        height: 60px;
        border: none;
        outline: none;
        font-size: 21px;
      }
    }
    .content-warp{
      margin-top: 15px;
      .ul{
        width: 800px;
        margin: auto;
        .li{
          border: 1px solid #999999;
          display: flex;
          align-items: center;
          margin-bottom: 8px;
          border-radius: 8px;
          padding: 15px 60px;
          .li-left{
            flex: 1;
          }
          .li-content{
            flex: 10;
          }
          .optBtn{
            flex: 1;
            border: 1px solid;
            text-align: center;
            background: red;
            color: white;
            display: none;
          }
        }
        .li:hover{
          background: #f5f5f5;
          .optBtn{
            display: block;
          }
        }
      }
    }
    .bottom{
      display: flex;
      align-items: center;
      width: 800px;
      margin: auto;
      .bottom-div{
        margin-right: 20px;
      }
      .bottom-div2{
        background: red;
        color: white;
      }
    }
  }
</style>
